package larpplanner.gui;

import larpplanner.database.FicCharacter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.cloudgarden.resource.SWTResourceManager;

public class CharDltWin extends org.eclipse.swt.widgets.Dialog{

	private GUISafeLARPManager mngr = GUISafeLARPManager.get();
	
	private Shell dialogShell;
	private UniverseCharacterPanel univCharPanel;
	private Button okBtn;
	
	public CharDltWin(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		dialogShell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		SWTResourceManager.registerResourceUser(dialogShell);
		dialogShell.setLayout(new GridLayout());
		
		univCharPanel = new UniverseCharacterPanel(dialogShell, SWT.NONE, null);
		okBtn = new Button(dialogShell, SWT.NONE);
			
		GridData okBtnLData = new GridData();
		okBtnLData.horizontalAlignment = GridData.FILL;
		okBtn.setLayoutData(okBtnLData);
		okBtn.setText("Delete");
		
		okBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if(univCharPanel.getCharComboIndex() < 1) {
					PopMessage.errorMessage("must select a character to delete");
					return;
				}
				mngr.del(FicCharacter.class, univCharPanel.getCharId(), new SafeCallback<Boolean>() {
					public void onReturn(Boolean result) {
						dialogShell.dispose();
					}
				});
			}
		});
		
		dialogShell.layout();
        dialogShell.pack();            
        dialogShell.setText("Select character to delete");
        
        Display display = dialogShell.getDisplay();
        Point p = display.getCursorLocation();
        p.x -= dialogShell.getSize().x / 2;
        dialogShell.setLocation(p);
        dialogShell.open();
        while (!dialogShell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
	}
	
	public void setOkBtnEnabled(boolean enabled) {
		okBtn.setEnabled(enabled);
	}
	
}
